<template>

  <div class="database-form">
    <el-form :inline="false" ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item
        :prop="item.model"
        v-for="(item, index) in forms"
        :key="index"
        :label="item.label">
        <el-input v-model="form[item.model]"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{button}}</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  name: 'add',
  data () {
    return {
      api: {
        add: this.$api.database.add,
        detail: this.$api.database.detail
      },
      tables: [],
      form: {
      },
      activeName: null,
      rules: {

      },
      button: '初始化数据库',
      forms: [
        {
          label: '数据库名',
          model: 'name',
          default: '',
          necessary: {
            message: '请输入数据库的名称如: test'
          }
        },
        {
          label: '数据库账号',
          model: 'account',
          default: null,
          necessary: {
            message: '请输入数据库账号: test'
          }
        },
        {
          label: '数据库密码',
          model: 'password',
          default: null,
          necessary: {
            message: '请输入密码: test'
          }
        },
        {
          label: '数据库端口',
          model: 'port',
          default: null,
          necessary: {
            message: '请输入端口号: 3306'
          }
        },
        {
          label: '数据库地址',
          model: 'address',
          default: null,
          necessary: {
            message: '请输入地址: 127.0.0.1'
          }
        }
      ]
    }
  },
  methods: {
    init () {
      for (let i = 0; i < this.forms.length; i++) {
        let temp = this.forms[i].necessary
        if (temp) {
          this.rules[this.forms[i].model] = [{ required: true, message: temp.message, trigger: 'blur' }]
        }
        let defaultValue = this.forms[i].default
        if (defaultValue) {
          this.form[this.forms[i].model] = defaultValue
        }
      }
    },
    handleDetail () {
      this.$httpf({ url: this.api.detail }, data => {
        if (data.data.data.detail) {
          this.form = data.data.data.detail
          this.button = '更新数据库'
        }
      })
    },
    onSubmit () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$httpf({ url: this.api.add, paras: this.form }, data => {
            this.button = '此数据库已经创建'
            this.$router.go(-1)
          })
        }
      })
    }
  },
  created () {
    this.init()
    this.handleDetail()
  }
}
</script>

<style scoped lang="less">

  .database-form {
    display: flex;
    flex-direction: column;
    height: 100%;
    box-sizing: border-box;
    padding-right: 10px;
    margin-top: 20px;
    .edit {
      flex: 1 1 100%;
      overflow: auto;
      width: 40%;
      margin: auto;
      height: 30%;
    }
    .tables {
      padding-left: 10px;
      display: flex;
      flex-direction: column;
      width: 500px;
      margin: 10px auto;

      .tabs {
        padding: 10px;
        box-shadow: rgba(0, 0, 0, 0.1) 0px 2px 12px 0px;
        width: 100%;
        box-sizing: border-box;
        background-color: rgb(255, 255, 255);
      }
    }
    .text {
      font-size: 14px;
    }

    .item {
      margin-bottom: 18px;
    }

    .clearfix:before,
    .clearfix:after {
      display: table;
      content: "";
    }
    .clearfix:after {
      clear: both
    }

    .box-card {
      width: 100%;
      box-sizing: border-box;
      margin-right: 10px;
    }

  }

</style>
